<template>
	<view>
		<wyb-loading ref="loading" />
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	import {
		wxPay,
		pcCheckPay
	} from "@/api/apiList/user.js"
	import wybLoading from '@/components/wyb-loading/wyb-loading.vue'

	export default {
		components: {
			wybLoading
		},
		data() {
			return {
				id: ""
			}
		},
		methods: {
			async API_wxPay(id) { //微信支付
				let result = {
					param: {
						id,
						app: 1 //1-小程序、2-pc
					}
				}
				return await wxPay(result)
			},
			async pay(data) {
				return new Promise((resolve, reject) => {
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: data.timeStamp,
						nonceStr: data.nonceStr,
						package: data.packageValue,
						signType: data.signType,
						paySign: data.paySign,
						success: function(res) {
							resolve(res)
						},
						fail: function(err) {
							reject(err)
						}
					});
				})
			}
		},
		async onLoad(option) {
			this.$refs.loading.showLoading()
			this.id = option.id
			let res = await this.API_wxPay(this.id)

			if (res.data.code === 1000) {
				this.$refs.loading.hideLoading()

				let payRes = await this.pay(res.data.data)
				console.log(payRes)
				if (payRes.errMsg === "requestPayment:ok") { //支付成功
					this.$refs.uToast.show({
						title: '支付成功',
						type: 'success',
						url: '/pages/signup/signup',
						isTab:true
					})
				}else{
					this.$refs.uToast.show({
						title: '支付失败',
						type: 'error',
						url: '/pages/signup/signup',
						isTab:true
					})
				}
			}else{
				this.$refs.uToast.show({
					title: res.data.msg,
					type: 'error',
					url: '/pages/signup/signup',
					isTab:true
				})
			}
		}
	}
</script>

<style>
</style>
